// 获取景点列表
const attractionsService = require("../services/attractionsService");

const getAttractionsList = async (ctx) => {
  try {
    const { page = 1, limit = 10, keyword, category } = ctx.query;
    const attractions = await attractionsService.getAttractionsList(page, limit, keyword, category);
    ctx.body = {
      code: 200,
      message: '获取成功',
      data: attractions
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: 'Internal Server Error' };
  }
};

// 获取景点详情
const getAttractionById = async (ctx) => {
  try {
    const { id } = ctx.params;
    const attraction = await attractionsService.getAttractionById(id);
    ctx.body = {
      code: 200,
      message: '获取成功',
      data: attraction
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: 'Internal Server Error' };
  }
};

// 添加新景点
const addAttraction = async (ctx) => {
  try {
    const attractionData = ctx.request.body;
    const newAttraction = await attractionsService.addAttraction(attractionData);
    ctx.status = 201;
    ctx.body = {
      code: 201,
      message: '新增成功',
      data: newAttraction
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: 'Internal Server Error' };
  }
};

// 更新景点信息
const updateAttraction = async (ctx) => {
  try {
    const { id } = ctx.params;
    const updateData = ctx.request.body;
    await attractionsService.updateAttraction(id, updateData);
    ctx.body = {
      code: 200,
      message: '更新成功'
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: 'Internal Server Error' };
  }
};

// 删除景点
const deleteAttraction = async (ctx) => {
  try {
    const { id } = ctx.params;
    await attractionsService.deleteAttraction(id);
    ctx.body = {
      code: 200,
      message: '删除成功'
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: 'Internal Server Error' };
  }
};

// 添加评论
const addComment = async (ctx) => {
  try {
    const { id } = ctx.params;
    const commentData = ctx.request.body;
    await attractionsService.addComment(id, commentData);
    ctx.status = 201;
    ctx.body = {
      code: 201,
      message: '评论成功'
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: 'Internal Server Error' };
  }
};

// 获取景点评论
const getComments = async (ctx) => {
  try {
    const { id } = ctx.params;
    const comments = await attractionsService.getComments(id);
    ctx.body = {
      code: 200,
      message: '获取成功',
      data: comments
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: 'Internal Server Error' };
  }
};

module.exports = {
  getAttractionsList,
  getAttractionById,
  addAttraction,
  updateAttraction,
  deleteAttraction,
  addComment,
  getComments
};
